package other;

import java.math.BigInteger;
import java.util.Scanner;

/**
 * @Author: Dang Qi
 * @Date: 2021/4/19  23:07
 * @Description: 字符串相加，相减，相乘
 */
public class StringPlusString {
    public static void main(String[] args) {
//        startAdd();
        String s1 = "45366666662111523545495485635435434189778894";
        String s2 = "89419999999999534534535466666664181315164694";
        System.out.println(new BigInteger(s1).multiply(new BigInteger(s2)).toString().equals(multi(s1, s2)));

    }
    public static void startAdd(){
        Scanner sc = new Scanner(System.in);
        String s1 , s2 ;
        while(true){
            if((s1 = sc.next()).equals("exit")) break;
            if((s2 = sc.next()).equals("exit")) break;
            System.out.println(add(s1, s2));
        }
    }
    //相乘
    public static String multi(String s1, String s2) {
        int end = s2.charAt(0)=='-' ? 1 : 0;
        String ans = "0";
        StringBuilder suff = new StringBuilder();
        for(int i = s2.length()-1; i >= end; i--){
            String s0 = multi(s1, s2.charAt(i)-'0')+suff;
            ans = add(ans, s0);
            suff.append("0");
        }
        if(end==0) return ans;
        return s1.charAt(0)=='-' ? ans.substring(1) : "-"+ans;
    }

    public static String multi(String s, int n) {
        if (n == 0) return "0";
        String resFlag = "";
        if (s.charAt(0) == '-') {
            resFlag = "-";
            s = s.substring(1);
        }
        int jin = 0;
        StringBuilder res = new StringBuilder();
        for(int i = s.length()-1; i >= 0; i--){
            int num = jin + (s.charAt(i) - '0') * n;
            jin = num / 10;
            num = num % 10;
            res.append(num);
        }
        if(jin!=0){
            res.append(jin);
        }
        return resFlag+res.reverse();
    }
    //相加
    public static String add (String s1, String s2){
        //表示要计算的是加法或是除法
        boolean plus = false;
        //相加的两个数
        String one, two;
        //两个符号如果都是-，则 最后结果需要乘以-1
        int resFlag = 1;
        //表示是否交换过
        boolean isExchange = false;
        if(s1.charAt(0)=='-'&&s2.charAt(0)=='-'){
            plus = true;resFlag = -1;
            one = s1.substring(1);
            two = s2.substring(1);
        }else if(s1.charAt(0)=='-'&&s2.charAt(0)!='-'){
            one = s2;
            two = s1.substring(1);
        }else if(s2.charAt(0)=='-'){
            one = s1;
            two = s2.substring(1);
        }else{
            plus = true;
            one = s1;
            two = s2;
        }
        //让one表示的数更大
        if(one.compareTo(two)<0) {
            isExchange = true;
            String s = one;
            one = two;
            two = s;
        }
        int i = one.length()-1, j = two.length()-1;
        //表示进位
        int jin = 0;
        //如果是加法的话 x = a + isPlus*b
        int isPlus = plus ? 1: -1;
        StringBuilder res = new StringBuilder("");

        while(i>=0||j>=0){
            int num =  jin;
            if(i>=0) num += one.charAt(i) - '0';
            if(j>=0) num += isPlus * (two.charAt(j) - '0');
            //进位，如果是减法， jin为负数， 需要向前面借位
            jin = num < 0 ? -1 : (num >= 10 ? 1 : 0);
            num = (num + 10) % 10;
            res.append(num);
            i--;j--;
        }
        if(jin!=0){
            if(jin < 0) res.append((-jin)+"-");
            else res.append(jin);
        }

        res = res.reverse();
        int k=0; //找到从左边数第一个不是零的
        for(k = 0; k < res.length(); k++){
            if(res.charAt(k)!='0'){
                char ch = res.charAt(k);
                resFlag = resFlag *  (isExchange&&!plus?-1:1)* (ch=='-'?-1:1);
                break;
            }
        }
        String ans = null;
        if(resFlag>0) ans = res.substring(k);
        else{
            if(res.charAt(k)=='-') ans = res.substring(k+1);
            ans = "-"+res.substring(k);
        }
        return ans.equals("") ? "0" : ans;
    }

}
